package com.daiqile.mydesignview.MyView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.daiqile.mydesignview.R;
import com.daiqile.mydesignview.Util.DisplayUtil;

/**
 * @author Mr.Bean
 * @date Created on 2017/10/9.
 */

public class MyTopBar extends RelativeLayout {
    private TextView title;
    private ImageView leftbtn,rightbtn;
    private int titleColor ;
    private Drawable leftBackground,rightBackground;
    private String titleText;
    private float titleSize;
    private  LayoutParams leftParams, rightParams, titleParams;
    public MyTopBar(Context context, AttributeSet attrs){
        super(context,attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTopBar);
        titleColor = typedArray.getColor(R.styleable.MyTopBar_titleColor,0);
        leftBackground = typedArray.getDrawable(R.styleable.MyTopBar_leftBackground);
        rightBackground = typedArray.getDrawable(R.styleable.MyTopBar_rightBackground);
        titleText = typedArray.getString(R.styleable.MyTopBar_titleText);
        titleSize = typedArray.getDimension(R.styleable.MyTopBar_titleSize,10);
        typedArray.recycle();

        title = new TextView(context);
        leftbtn = new ImageView(context);
        rightbtn = new ImageView(context);


        leftbtn.setImageDrawable(leftBackground);
        leftbtn.setPadding(DisplayUtil.dip2px(context, 16), 0, DisplayUtil.dip2px(context, 16), 0);
        rightbtn.setImageDrawable(rightBackground);
        rightbtn.setPadding(DisplayUtil.dip2px(context, 16), 0, DisplayUtil.dip2px(context, 16), 0);
        title.setText(titleText);
        title.setTextColor(titleColor);
//        title.setTextSize(TypedValue.COMPLEX_UNIT_PX,titleSize);
        //和上一句作用相同
        title.setTextSize(0,titleSize);



        leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
        leftParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE);
        addView(leftbtn,leftParams);

        rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
        addView(rightbtn,rightParams);

        titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
        addView(title,titleParams);



        leftbtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                myTopBarClickListener.leftClick();
            }
        });
        rightbtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                myTopBarClickListener.rightClick();
            }
        });

    }

    private MyTopBarClickListener myTopBarClickListener;
    public interface MyTopBarClickListener{
        public void leftClick();
        public void rightClick();

    }
    public void setOnMyTopBarClickListen(MyTopBarClickListener listener){
        this.myTopBarClickListener = listener;
    }

   /* leftbtn.setOnMyTopBarClickListen(new OnClickListener(){
        @Override
               public void onClick(View v){
            myTopBarClickListener.leftClick();
        }
    });*/

}
